/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/trapping-rain-water
   @Language: C++
   @Datetime: 19-06-06 14:10
   */

class Solution {
public:
	int trap(vector<int>& height) {
		int water=0;
		for(int l=0, lm=0, r=height.size()-1, rm=0; l<r;){
			lm=max(lm,height[l]);
			rm=max(rm,height[r]);
			if(lm<rm) water+=lm-height[l++];
			else water+=rm-height[r--];
		}
		return water;
	}
};
